package com.aplink.generic.widgets;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.adapter.ConfigAdapter;
import com.aplink.generic.constant.BundleConstant;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.ColumnSetting;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjectFilter;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.gridview.GridViewSpec;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.convertcount.GenericFilter;
import com.aplink.generic.util.objectrule.TargetRuleCollection;
import com.aplink.generic.xmlparser.ColumnSettingParser;
import com.aplink.generic.xmlparser.ContentDataParser;

public class ApLinkGridView extends ApLinkBaseView {
    /**
     * 
     */
    private static final long serialVersionUID = 8903183721406101505L;
    private ArrayList<ColumnSetting> columnSettings;
    private ConfigAdapter mGenericConfigAdapter;
    private ArrayList<GenericObject> mGenericObjects;
    private final GridView mGridView;
    private final GridViewSpec mGridViewSpecFormat;
    private final OnItemClickListener mOnRowClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(final AdapterView<?> arg0, final View arg1,
                final int index, final long arg3) {
            if (ApLinkGridView.this.targetCollection != null) {
                String fullClassName = null;
                try {
                    final GenericObject genericObject = ApLinkGridView.this.mGenericConfigAdapter
                            .getDisplayObjects().get(index);

                    fullClassName = GenericFilter
                            .getTargetActivityFromRule(
                                    ApLinkGridView.this.targetCollection,
                                    genericObject);
                    if (fullClassName == null) {
                        fullClassName = ApLinkGridView.this.targetCollection
                                .getDefaultTarget();
                    }
                    final Class<?> targetActivity = Class
                            .forName(fullClassName);
                    final Intent intent = new Intent(
                            ApLinkGridView.this.getApplicationContext(),
                            targetActivity);
                    final Bundle bundle = new Bundle();
                    bundle.putSerializable(BundleConstant.LIST_OBJECT,
                            ApLinkGridView.this.mGenericObjects);
                    bundle.putInt(BundleConstant.SELECTED_INDEX, index);
                    bundle.putString(BundleConstant.PARENT, ApLinkGridView.this
                            .getGenericFragment().getClassName());
                    intent.putExtras(bundle);
                    ApLinkGridView.this.getActivity().startActivity(intent);
                } catch (final ClassNotFoundException e) {
                    LogUtil.error(fullClassName + " is not found",
                            "Grid view item click", getClass());
                }
            }
        }
    };

    private final TargetRuleCollection targetCollection;

    public ApLinkGridView(final GenericFragment genericFragment,
            final ControlSpec controlSpec, final View view) {
        super(genericFragment, controlSpec, view);
        this.mGridView = (GridView) view;
        this.mGridViewSpecFormat = (GridViewSpec) controlSpec;
        this.targetCollection = this.mGridViewSpecFormat
                .getTargetRuleCollection();
    }

    @Override
    public void clear() {

    }

    public void filter(final String filterValue) {

    }

    private ConfigAdapter generateConfigAdapter() {
        this.columnSettings = ColumnSettingParser.parse(getGenericFragment(),
                this.mGridViewSpecFormat.getRptrdesignID());
        return new ConfigAdapter(getGenericFragment(), this.mGenericObjects,
                this.columnSettings, this.mGridViewSpecFormat.getRowItem());
    }

    @Override
    public GenericAttribute getAttribute() {
        return null;
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public GridView getView() {
        return this.mGridView;
    }

    @Override
    protected void initViewProperties() {
        final ObjectFilter filter = this.mGridViewSpecFormat.getFilter();

        this.mGenericObjects = loadData(filter);
        if (this.mGenericObjects != null) {
            this.mGenericConfigAdapter = generateConfigAdapter();
            this.mGridView.setAdapter(this.mGenericConfigAdapter);
        }
    }

    @Override
    protected void loadBindingObject() {

    }

    private ArrayList<GenericObject> loadData(final ObjectFilter filter) {
        final ContentData objPropCol = ContentDataParser.getInstance()
                .parseIntoObjProp(this.mGridViewSpecFormat.getContentDataID());
        if ((filter != null) && (filter.getFilterValue() != null)
                && !filter.isShowAll()) {
            final String whereClause = SqlMethods
                    .getInstance()
                    .equal(filter.getDataObject() + "."
                            + filter.getMappingName(), filter.getFilterValue())
                    .toString();
            return getDatabaseGet().getList(objPropCol, whereClause);
        } else {
            return getDatabaseGet().getList(objPropCol, null);
        }
    }

    @Override
    protected void loadDefaultObject() {

    }

    @Override
    protected void setEventListener() {
        this.mGridView.setOnItemClickListener(this.mOnRowClickListener);
    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }
}
